<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir . "/inc/gruppenkinder.php";
require_once $basedir . "/inc/gruppen.php";

if ($format == "excel") {
    $date = date("Y-m-d");
    if (isset($_REQUEST['stichtag_am'])) {
        $date = $_REQUEST['stichtag_am'];
    }
    $list = array();
    if (isset($_REQUEST['gruppen'])) {
        foreach ($_REQUEST['gruppen'] as $gruppe) {
            $temp = GruppenKinder::getAll(true, $gruppe, $date);
            $list = array_merge($list, $temp);
        }
    }
    require_once $basedir . "/external/PHPExcel/Classes/PHPExcel.php";

    $objPHPExcel = new PHPExcel();
    PHPExcel_Settings::setLocale("de");
    $worksheet = $objPHPExcel->getActiveSheet();
    /*
     * Create Table Header
     */
    $worksheet->setCellValueByColumnAndRow(0, 1, "");
    $worksheet->setCellValueByColumnAndRow(1, 1, "Name");
    $worksheet->setCellValueByColumnAndRow(2, 1, "Vorname");
    $worksheet->setCellValueByColumnAndRow(3, 1, "Geb.");
    $worksheet->setCellValueByColumnAndRow(4, 1, "Straße");
    $worksheet->setCellValueByColumnAndRow(5, 1, "Nr.");
    $worksheet->setCellValueByColumnAndRow(6, 1, "Zus.");
    $worksheet->setCellValueByColumnAndRow(7, 1, "von uns");
    $worksheet->setCellValueByColumnAndRow(8, 1, "PLZ");
    $worksheet->setCellValueByColumnAndRow(9, 1, "Betr. von");
    $worksheet->setCellValueByColumnAndRow(10, 1, "bis");
    $worksheet->setCellValueByColumnAndRow(11, 1, "Migration");
    $worksheet->setCellValueByColumnAndRow(12, 1, "Zurückgestellt bei");
/*    $worksheet->setCellValueByColumnAndRow(13, 1, "Monatsbeitrag");
    $worksheet->setCellValueByColumnAndRow(14, 1, "Spende p.M.");
    $worksheet->setCellValueByColumnAndRow(15, 1, "Hygienebeitrag");
    $worksheet->setCellValueByColumnAndRow(16, 1, "Baugeld");
*/    /*
     * Create Content to Table
     */
    $row = 0;
    for ($i = 0; $i < count($list); $i++) {
        $value = $list[$i];
        $kind = $list[$i]->getKind();
        $adress = $kind->getAdresse();
        $vertrag = $kind->getCurrentContract();
        $row = $i + 2;
        $worksheet->setCellValueByColumnAndRow(0, $row, "Bremer Str.");
        $worksheet->setCellValueByColumnAndRow(1, $row, utf8_encode($kind->getNachname()));
        $worksheet->setCellValueByColumnAndRow(2, $row, utf8_encode($kind->getVorname()));
        $worksheet->setCellValueByColumnAndRow(3, $row, date("d.m.Y", strtotime($kind->getGeburtsdatum())));
        $worksheet->setCellValueByColumnAndRow(4, $row, utf8_encode($adress->getStrasse()));
        $worksheet->setCellValueByColumnAndRow(5, $row, $adress->getHausnummer());
        $worksheet->setCellValueByColumnAndRow(6, $row, $adress->getZusatzHausnummer());
        $worksheet->setCellValueByColumnAndRow(8, $row, $adress->getPlz());
        if ($vertrag != null) {
            $worksheet->setCellValueByColumnAndRow(9, $row, $vertrag->getBetreuungVon());
            $worksheet->setCellValueByColumnAndRow(10, $row, $vertrag->getBetreuungBis());
        }
        $worksheet->setCellValueByColumnAndRow(11, $row, $kind->isMigration() ? "X" : "");
        $worksheet->setCellValueByColumnAndRow(12, $row, utf8_encode($kind->getZurueckgestellt()));
/*        $worksheet->setCellValueByColumnAndRow(13, $row, $vertrag->getMonatsbeitrag());
        $worksheet->setCellValueByColumnAndRow(14, $row, $vertrag->getSpende());
        $worksheet->setCellValueByColumnAndRow(15, $row, $vertrag->getHygienebeitrag());
        $worksheet->setCellValueByColumnAndRow(16, $row, $vertrag->getBaugeld());
*/    }
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit ;
}

$gruppen = Gruppen::getAll();
?>
<form method='post' action="/excel.php">
<input type="hidden" name="page" value="<?php echo $menuid; ?>" />
<div>
<div class='inline-block label'>Gruppen:</div>
<div class='inline-block'>
<select name='gruppen[]' size="4" multiple="multiple">
<?php
foreach($gruppen as $gruppe) {
?>
<option value='<? echo $gruppe -> getId(); ?>'><? echo $gruppe -> getName(); ?></option>
<?php
}
?>
</select>
</div>
</div>
<div>
<div class='inline-block label'>Stichtag:</div>
<div class='inline-block'>
<input type='date' name='stichtag_am' value=''/>
</div>
</div>
<div>
<input type='submit' name='meldeliste' value='Meldeliste'/>
</div>
</form>
